package com.hnhy.hnhyscms_ws.controller;

import com.hnhy.hnhyscms_ws.entity.Customers;
import com.hnhy.hnhyscms_ws.entity.Orders;
import com.hnhy.hnhyscms_ws.service.ICustomersService;
import com.hnhy.hnhyscms_ws.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/orders")
public class OrderController {
    @Autowired
    private IOrdersService iOrdersService;
    @Autowired
    private ICustomersService iCustomersService;
    @RequestMapping(value = "/getAllOrders",method= RequestMethod.GET)
    private List<Orders> getAllOrders(){ return iOrdersService.getAllOrders(); }
    @RequestMapping(value = "/getByOrderNo",method = RequestMethod.POST)
    public Orders getByOrderNo(@RequestParam String orderNo){ return  iOrdersService.getByOrderNo(orderNo);}
    @RequestMapping(value = "/getByCustomerNo",method = RequestMethod.POST)
    public List<Orders> getByCustomerNo(@RequestParam String customerNo){ return  iOrdersService.getByCustomerNo(customerNo);}
    @RequestMapping(value = "/updateType",method = RequestMethod.POST)
    public void updateType(@RequestParam String orderNo,@RequestParam Byte orderStatus){
        Orders orders= iOrdersService.getByOrderNo(orderNo);
        orders.setOrderStatus(orderStatus);
        iOrdersService.updateType(orders);
    }
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Orders addOrder(
            @RequestParam String customerNo,
            @RequestParam Float finaltotal,
            @RequestParam(value = "leadtime")  String leadtime,
            @RequestParam(value = "orderTime") String orderTime,
            @RequestParam Float amount,
            @RequestParam String remark,
            @RequestParam String invoiceInfo,
            @RequestParam String Logisticinfo)
    {
        Orders order=new Orders();
        Customers customer;
        Byte tmp=0;
        int orderid;
        order.setCustomerNo(customerNo);
        order.setFinalTotal(finaltotal);
        order.setLeadtime(leadtime);
        order.setOrderTime(orderTime);
        order.setAmount(amount);
        order.setRemark(remark);
        order.setInvoiceInfo(invoiceInfo);
        order.setLogisticinfo(Logisticinfo);
        customer=iCustomersService.getByCustomerNo(customerNo);
        order.setCustomerName(customer.getCustomerName());
        if(order.getInvoiceInfo()=="")
            order.setInvoiceInfo(customer.getInvoiceInfo());
        if(order.getLogisticinfo()=="")
            order.setLogisticinfo("联系人："+customer.getLinkPerson()+";联系电话："+ customer.getLinkPhone()+";公司地址："+ customer.getLinkAddr());
        order.setOrderStatus(tmp);
        order.setOrderType(tmp);
        orderid=(iOrdersService.findMaxId()).getOrderid();
        order.setOrderNo("shy"+"0000"+(orderid+1));
        iOrdersService.addOrders(order);
        return order;
    }
    @RequestMapping(value = "/seeAbout",method = RequestMethod.POST)
    @ResponseBody
    public  List<Orders> updateType(@RequestParam String customerNo,@RequestParam String createTime){
        return iOrdersService.seeAbout(customerNo,createTime);
    }
}
